package com.zhentao.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.zhentao.admin.dto.StudentAddDto;
import com.zhentao.admin.dto.StudentSelDto;
import com.zhentao.admin.dto.UserJinYongDto;
import com.zhentao.admin.pojo.SStudentTree;
import com.zhentao.admin.vo.StudentVo;
import com.zhentao.aop.MyLog;
import com.zhentao.domain.TUser;
import com.zhentao.listener.UploadDataListener;
import com.zhentao.result.Result;
import com.zhentao.service.SStudentTreeService;
import com.zhentao.service.TUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class UserManagerController {
    @Resource
    private TUserService tUserService;
    @Resource
    private SStudentTreeService sStudentTreeService;

    //展示所有学生分页
    @PostMapping("/studentLimit")
    public Result studentLimit(@RequestBody StudentSelDto studentSelDto){
       PageInfo<StudentVo> pageInfo = tUserService.selectStudentLimit(studentSelDto);
       return Result.OK(pageInfo);
    }

    //添加学生
    @PostMapping("/addStudent")
    @MyLog(title = "学生管理",content = "添加学生")
    public Result addStudent(@RequestBody StudentAddDto studentAddDto){
        Result result = tUserService.addStudent(studentAddDto);
        return result;
    }

    //批量删除学生
    @PostMapping("/delBatchStudent/{ids}")
    @MyLog(title = "学生管理",content = "批量删除学生")
    public Result delBatchStudent(@PathVariable("ids") Integer[] ids){
        Result result = tUserService.delBatchStudent(ids);
        return result;
    }

    //修改学生信息
    @PutMapping("/updateStudent")
    @MyLog(title = "学生管理",content = "修改学生信息")
    public Result updateStudent(@RequestBody TUser tUser){
        Result result = tUserService.updateStudent(tUser);
        return result;
    }

    //查询所有市场部
    @GetMapping("/allMarket")
    public Result allMarket(){
        Result result = tUserService.allMarket();
        return result;
    }

    //查询所有班级
     @GetMapping("/allClass")
     public Result allClass(){
         Result result = tUserService.allClass();
         return result;
     }

     //批量导入学生信息
     @PostMapping("/upload")
     @MyLog(title = "学生管理",content = "批量导入学生信息")
     public Result upload(MultipartFile file) throws IOException {
         long t1 = System.currentTimeMillis();
         EasyExcel.read(file.getInputStream(), TUser.class, new UploadDataListener(tUserService)).sheet().doReadSync();
         long t2 = System.currentTimeMillis();
         System.out.println(file.getOriginalFilename());
         return Result.OK("导入成功，共用时:"+(t2-t1));
     }

     //导出学生信息
    @GetMapping("/download")
    @MyLog(title = "学生管理",content = "导出学生信息")
    public Result export(HttpServletResponse response) throws IOException {
        tUserService.download(response);
        return Result.OK();
    }

    //禁用账号
    @PostMapping("/disable")
    @MyLog(title = "学生管理",content = "禁用账号")
     public Result disable(@RequestBody UserJinYongDto userJinYongDto){
         Result result = tUserService.disable(userJinYongDto);
         return result;
    }

    //重置密码
    @GetMapping("/resetPassword/{userId}")
    @MyLog(title = "学生管理",content = "重置密码")
     public Result resetPassword(@PathVariable("userId") Integer userId){
         Result result = tUserService.resetPassword(userId);
         return result;
    }

    //树形查询
    @GetMapping("stu_list_tree")
    public Result stu_list_tree(){
        List<SStudentTree> treeList = sStudentTreeService.treslist();
        return Result.OK(treeList);
    }
}